package logic

import (
	"context"
	"fmt"
	"qianhui/models/mysqlinit"
	"qianhui/pkg"

	"qianhui/qianhuirpc/internal/svc"
	"qianhui/qianhuirpc/qianhuirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ResetPassWordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewResetPassWordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetPassWordLogic {
	return &ResetPassWordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ResetPassWordLogic) ResetPassWord(in *qianhuirpc.ResetPassWordRequest) (*qianhuirpc.ResetPassWordResponse, error) {
	// todo: add your logic here and delete this line
	var reset mysqlinit.User
	mysqlinit.DB.Debug().Where("account=?", in.Account).Find(&reset)
	if reset.ID == 0 {
		return nil, fmt.Errorf("账号不存在，请先注册")
	}

	if reset.Password != pkg.Md5Password(in.Password) {

		return nil, fmt.Errorf("原密码码错误，请重新输入")

	}
	if in.Newpassword != in.Newspassword {

		return nil, fmt.Errorf("两次密码不一致，请重新输入")

	}
	if in.Password == in.Newpassword {
		return nil, fmt.Errorf("两次密码不能与旧密码相同，请重新输入")
	}
	reset.Password = pkg.Md5Password(in.Newpassword)
	mysqlinit.DB.Debug().Updates(&reset)
	return &qianhuirpc.ResetPassWordResponse{
		Id: int64(reset.ID),
	}, nil
}
